/*
 * Copyright (c) 2018. Beyondsoft Corporation.
 * All Rights Reserved.
 *
 * BEYONDSOFT CONFIDENTIALITY
 *
 * The information in this file is confidential and privileged from Beyondsoft Corporation,
 * which is intended only for use solely by Beyondsoft Corporation.
 * Disclosure, copying, distribution, or use of the contents of this file by persons other than Beyondsoft Corporation
 * is strictly prohibited and may violate applicable laws.
 */

package com.liancheng.smarthome.bean;

import com.httpapi.BaseResultEntity;
import com.liancheng.smarthome.utils.appcommon.AppCommonUtils;
import com.liancheng.smarthome.utils.constant.FileConstant;
import com.liancheng.smarthome.utils.constant.UserConstant;
import com.liancheng.smarthome.utils.fileio.SharedPreferencesUtil;

import java.util.List;

/**
 * @author xzk
 * @data 2019/2/26
 * @email xiezhengkun@beyondsoft.com
 * @remark 员工登陆实体类
 */
public class LoginBean extends BaseResultEntity<LoginBean> {
    //{
//        "jwt":"eyJhbGciOiJIUzUxMiJ9.eyJhdXRob3JpdGllcyI6IjY2NDQ4MDIxNjI1MjQxNiIsImNsYXNzTmFtZSI6ImNvbS5iZXlvbmRzb2Z0LnJkYy5jbG91ZC5icG8uZXNjb21tb24uYmFzaWMuc2VjdXJpdHkuYXV0aC51c2VyLm1vZGVsLlVzZXJBY2NvdW50Iiwib2JqZWN0Ijoie1widXNlcm5hbWVcIjpcImFkbWluXCIsXCJ1c2VySWRcIjpcIi0xXCIsXCJpc0V4cGlyZWRcIjpmYWxzZSxcImlzTG9ja2VkXCI6ZmFsc2UsXCJhdXRob3JpdGllc1wiOlt7XCJyb2xlXCI6XCI2NjQ0ODAyMTYyNTI0MTZcIn1dfSIsInN1YiI6ImFkbWluIiwiZXhwIjoxNTgxNzUyMDg5fQ.gL--sgJtZTzPWdze01UqnH57-LgtA-1i2vBNZr5iKy7n-vK2iE34WK8CFzZnr6bur9-TWqQeMMcycGyPR8ze3g",
//            "account":{
//        "username":"admin",
//                "userId":"-1",
//                "isExpired":false,
//                "isLocked":false,
//                "authorities":[
//        {
//            "role":"664480216252416"
//        }
//    			]
//    }
//    }
    public String jwt;
    public Account account;

    public static class Account {
        //"username":"admin",
        //                 "userId":"-1",
        //                 "isExpired":false,
        //                 "isLocked":false,
        //                 "authorities":[
        //                    {
        //                        "role":"664480216252416"
        //                    }
        //    			]
        public String username;
        public int userId;
        public boolean isExpired;
        public boolean isLocked;

        public List<Authorities> authorities;

        public static class Authorities {
            public String role;
        }
    }

    public static final String getUserJwt(){
        return SharedPreferencesUtil.getInstance().getString(UserConstant.EMPLOYEE_jwt);
    }

    public static final void saveUserId(int id){
        SharedPreferencesUtil.getInstance().put(UserConstant.EMPLOYEE_ID,id);
    }


    public static final void saveUserName(String name){
        SharedPreferencesUtil.getInstance().put(UserConstant.EMPLOYEE_USER_NAME,name);
    }
    public static final String getUserName(){
        return  SharedPreferencesUtil.getInstance().getString(UserConstant.EMPLOYEE_USER_NAME);
    }

    public static final int getUserId(){
        return SharedPreferencesUtil.getInstance().getInt(UserConstant.EMPLOYEE_ID);
    }

    /**
     * 保存用户信息
     */
    public void saveEmployee() {
        SharedPreferencesUtil.getInstance().put(UserConstant.EMPLOYEE_jwt, this.jwt);
        if (this.account != null) {

            SharedPreferencesUtil.getInstance().put(UserConstant.EMPLOYEE_ID, this.account.userId);
            SharedPreferencesUtil.getInstance().put(UserConstant.EMPLOYEE_USER_NAME, this.account.username);
            if (!AppCommonUtils.arrayIsEmtry(this.account.authorities)) {
                Account.Authorities authorities = this.account.authorities.get(0);
                SharedPreferencesUtil.getInstance().put(UserConstant.EMPLOYEE_roleid, authorities.role);
            }
        }
    }

    /**
     * 退出登陆时需要清除用户信息
     */
    public static void loginOutAndClearEmployee() {
        SharedPreferencesUtil.getInstance().put(UserConstant.EMPLOYEE_jwt, "");
        SharedPreferencesUtil.getInstance().put(UserConstant.EMPLOYEE_ID, 0);
        SharedPreferencesUtil.getInstance().put(UserConstant.EMPLOYEE_USER_NAME, "");
        SharedPreferencesUtil.getInstance().put(UserConstant.EMPLOYEE_roleid, "");
        SharedPreferencesUtil.getInstance().put(FileConstant.UserLoginStatus,false);

        //清除本用户保存的网关信息
    }
}
